/**
 * <p>Title: ServiceZMCT02.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-14 上午10:00:31  高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.ct.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.common.zm.cInterface.ct.DataInterface;
import com.baosight.sczx.zm.ct.domain.ZMCTSystem;


/**
 * <p>Title: ServiceZMCT02.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-14 上午10:00:31  高强  Create
 *</pre>
 */
public class ServiceZMCT02 extends ServiceEPBase {
	
	java.sql.Connection con ;
	/**
	 * 页面初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		if(outInfo.getBlock(EiConstant.resultBlock).getRowCount()!=0)
		{
			outInfo.set("mode", "update");
		}else
		{
			outInfo.set("mode", "insert");
		}
		outInfo.addBlock((new DataInterface()).getDsBlock("cmes.ct.ds", null));
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		EiInfo outInfo = super.query(inInfo, "ZMCTSystem.query", new ZMCTSystem());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CT_SYSTEM"));//主键
		boolean validate = validate(inInfo,"ZMCTDataInterface.query");
		if(!validate) {
			return inInfo;
		}else
		{
			EiInfo outinfo = super.insert(inInfo, "ZMCTSystem.insert");
			return outinfo;
		}
		
		
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		boolean validate = validate(inInfo,"ZMCTDataInterface.query");
		if(!validate) {
			return inInfo;
		}else
		{
			EiInfo outInfo = super.update(inInfo, "ZMCTSystem.update");
			changeInitialData(outInfo);//如果修改成功则重新刷新数据
			return outInfo;
		}
		
	}
	
	/**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.SYSTEMSETTING_SMS = 1;
		}
	}
	
	/**
	 * 新增和修改时校验名称编号简称不得重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName) {
		
	    Map<String, String> params = new HashMap<String, String>();
		String personDsKeyId = inInfo.getBlock("result").getCellStr(0, "fdPersonDs");
		String departmentKeyId = inInfo.getBlock("result").getCellStr(0, "fdDepartmentDs");
		String personSql = inInfo.getBlock("result").getCellStr(0, "fdPersonSql");
		String departmentSql = inInfo.getBlock("result").getCellStr(0, "fdDepartmentSql");
		
		params.put("fdKeyId", personDsKeyId);
	    if(check(inInfo,personDsKeyId,personSql,"人员信息"))
	    {
	    	params = new HashMap<String, String>();
	    	params.put("fdKeyId", departmentKeyId);
	    	return check(inInfo,departmentKeyId,departmentSql,"部门信息");
	    }else
	    {
	    	return false;
	    }
		
	}
	
	/**
	 * 测试数据库连接是否正确
	 * <p>Function: check</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-14 上午10:00:31  高强  
	 * </p>
	 *
	 */
	private boolean check(EiInfo inInfo, String keyId, String sql, String type)
	{
		DataInterface dataInterface = new DataInterface(keyId);
		if(!dataInterface.drive())
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("找不到"+type+"驱动程序类 ，加载驱动失败");
			return false;
		}
		if(!dataInterface.connect())
		{
			inInfo.setStatus(-1);
			inInfo.setMsg(type+"数据库连接失败");
			return false;
		}
		
		if(dataInterface.search(sql) == null)
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("请检查"+type+"sql语句的正确性");
			return false;
		}
		 if(!dataInterface.colseDs())
		 {
			 inInfo.setStatus(-1);
			 inInfo.setMsg(type+"数据库关闭连接失败");
			 return false;
		 }
		return true;
	}
}

